const db = require("../utils/db");

// 权限-获取管理员列表
exports.authorityListGet = async (params) => {
    return new Promise(((resolve, reject) => {
        let sql = '';
        if (params.admin_name && params.h_id) {
            sql = `select * from admin where admin_name like '%${params.admin_name}%' and h_id like '%${params.h_id}%'` 
        } else if (params.admin_name) {
            sql = `select * from admin where admin_name like '%${params.admin_name}%'` 
        } else if (params.admin_id) {
            sql = `select * from admin where admin_id like '%${params.admin_id}%'`
        }else if (params.h_id) {
            sql = `select * from admin where h_id like '%${params.h_id}%'`
        } else {
            sql = `select * from admin`
        }
        db.query(sql).then(res => {
            console.log(res, "authorityListGet sql查询成功结果")
            let result = {};
            result.list = res.slice((params.pageIndex - 1) * (params.pageSize), (params.pageIndex - 1) * (params.pageSize) + params.pageSize);
            result.total = res.length;
            result.pageIndex = params.pageIndex;
            result.pageSize = params.pageSize;
            resolve(result);
        }).catch(error => {
            console.log(error, "authorityListGet sql查询失败结果")
            reject(error)

        })
    }))
}

// 权限-修改管理员信息
exports.authorityUpdate = async (params) => {
    return new Promise(((resolve, reject) => {
        var str = [], sql = '';
        for (var item in params) {
            str.push(`${item}='${params[item]}'`);
        }
        console.log(str.join(','));
        sql += `update admin set ` + str.join(',') + ` where admin_id = '${params.admin_id}'`;
        db.query(sql).then(res => {
            console.log(res, "authorityUpdate sql修改成功结果")
            let result = {};
            result.msg = '修改成功!'
            resolve(result);
        }).catch(error => {
            console.log(error, "authorityUpdate sql查询失败结果");
            reject(error)

        })
    }))
}